package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseCategoryMapper;
import com.xuecheng.content.mapper.CourseMarketMapper;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseCategory;
import com.xuecheng.content.model.po.CourseMarket;
import com.xuecheng.content.service.CourseBaseInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author :chenjie
 * @date :Created 2023/1/31 10:05
 */
@Transactional
@Service
public class CourseBaseInfoServiceImpl extends ServiceImpl<CourseBaseMapper,CourseBase> implements CourseBaseInfoService {
    @Autowired
    CourseBaseMapper courseBaseMapper;
    @Autowired
    CourseMarketMapper courseMarketMapper;
    @Autowired
    CourseCategoryMapper courseCategoryMapper;
    @Autowired
    CourseMarketServiceImpl courseMarketService;

    @Override
    public PageResult<CourseBase> queryCourseBaseList(PageParams pageParams, QueryCourseParamsDto queryCourseParams) {
        LambdaQueryWrapper<CourseBase> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(queryCourseParams.getCourseName()),CourseBase::getName,queryCourseParams.getCourseName());
        queryWrapper.eq(StringUtils.isNotEmpty(queryCourseParams.getAuditStatus()),CourseBase::getAuditStatus,queryCourseParams.getAuditStatus());
        queryWrapper.eq(StringUtils.isNotEmpty(queryCourseParams.getPublishStatus()),CourseBase::getStatus,queryCourseParams.getPublishStatus());


        Page<CourseBase> page=new Page<>(pageParams.getPageNo(),pageParams.getPageSize());
        Page<CourseBase> pageResult = courseBaseMapper.selectPage(page, queryWrapper);
        List<CourseBase> items = pageResult.getRecords();
        long total = pageResult.getTotal();
        return new PageResult<>(items, total, pageParams.getPageNo(), pageParams.getPageSize());

    }
    @Override
    public CourseBaseInfoDto createCourseBase(AddCourseDto dto,Long companyId) {
        //参数校验
//        if (StringUtils.isBlank(dto.getName())) {
//            XueChengPlusException.cast("课程名称为空");
//        }
//        if (StringUtils.isBlank(dto.getMt())) {
//            throw new RuntimeException("课程分类为空");
//        }
//        if (StringUtils.isBlank(dto.getSt())) {
//            throw new RuntimeException("课程分类为空");
//        }
//        if (StringUtils.isBlank(dto.getGrade())) {
//            throw new RuntimeException("课程等级为空");
//        }
//        if (StringUtils.isBlank(dto.getTeachmode())) {
//            throw new RuntimeException("教育模式为空");
//        }
//        if (StringUtils.isBlank(dto.getUsers())) {
//            throw new RuntimeException("适应人群为空");
//        }
//        if (StringUtils.isBlank(dto.getCharge())) {
//            throw new RuntimeException("收费规则为空");
//        }
        //参数封装
        CourseMarket courseMarket=new CourseMarket();
        CourseBase courseBase=new CourseBase();

        BeanUtils.copyProperties(dto,courseBase);
        courseBase.setCompanyId(companyId);
        BeanUtils.copyProperties(dto,courseMarket);

        if((dto.getCharge().equals("201001")&&courseMarket.getPrice()==null)||courseMarket.getPrice().floatValue()<=0){//逻辑校验
            XueChengPlusException.cast("收费异常");
        }
        courseBase.setCreateDate(LocalDateTime.now());
        courseBase.setAuditStatus("202002");
        courseBase.setStatus("203001");
        //插入新增数据
        int insert = courseBaseMapper.insert(courseBase);
        courseMarket.setId(courseBase.getId());
        int insert1 = courseMarketMapper.insert(courseMarket);
        if(insert<=0||insert1<=0){
            throw new RuntimeException("添加失败");
        }


        return getCourseBaseInfo(courseBase.getId());
    }

    @Override
    public CourseBaseInfoDto getCourseBaseById(Long id) {
        return getCourseBaseInfo(id);
    }

    @Override
    public CourseBaseInfoDto modifyCourseBase(EditCourseDto dto,Long companyId) {
        Long id = dto.getId();
        CourseBase courseBase = courseBaseMapper.selectById(id);
        if(courseBase==null){
            XueChengPlusException.cast("课程不存在");
        }
        if(!courseBase.getCompanyId().equals(companyId)){
            XueChengPlusException.cast("只能修改本机构课程");
        }
        BeanUtils.copyProperties(dto,courseBase);
        courseBase.setChangeDate(LocalDateTime.now());
        CourseMarket courseMarket = new CourseMarket();
        BeanUtils.copyProperties(dto,courseMarket);
        if((dto.getCharge().equals("201001")&&courseMarket.getPrice()==null)||courseMarket.getPrice().floatValue()<=0){//逻辑校验
            XueChengPlusException.cast("收费异常");
        }
        courseMarketService.saveOrUpdate(courseMarket);
        courseBaseMapper.updateById(courseBase);
        return getCourseBaseInfo(courseBase.getId());
    }

    public CourseBaseInfoDto getCourseBaseInfo(Long courseId){
        CourseMarket courseMarket = courseMarketMapper.selectById(courseId);
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        CourseBaseInfoDto courseBaseInfoDto=new CourseBaseInfoDto();
        BeanUtils.copyProperties(courseBase,courseBaseInfoDto);
        if(courseMarket!=null){
            BeanUtils.copyProperties(courseMarket,courseBaseInfoDto);
        }


        String st = courseBase.getSt();
        String mt = courseBase.getMt();
        CourseCategory mtCategory = courseCategoryMapper.selectById(mt);
        CourseCategory stCategory = courseCategoryMapper.selectById(st);
        if(mtCategory!=null){
            String name = mtCategory.getName();
            courseBaseInfoDto.setMtName(name);
        }
        if(stCategory!=null){
            String name = stCategory.getName();
            courseBaseInfoDto.setStName(name);
        }
        return courseBaseInfoDto;
    }
//    private int SavaCourseMarket(){
//
//    }

}
